package com.ktjiaoyu.web.interceptor;

import com.ktjiaoyu.pojo.SysUser;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class AuthorizationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.从session中获取登录用户信息
        HttpSession session = request.getSession();
        SysUser loginUser = (SysUser)session.getAttribute("loginUser");
        //1.1 获取到了 有登录
        if(loginUser!=null){
            //放行
            return true;
        }else{//1.2 没有获取到 没有登录
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print("<script>alert('请先进行登陆，再进行后续操作！(Interceptor控制)');location.href='/login.jsp';</script>");

            return false;
        }

    }
}
